<template>
  <div>
    <a-form-model
      :model="form"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      ref="customerInfoForm"
      class="formStyle"
    >
      <a-row :gutter="10">
        <a-col :span="8">
          <a-form-model-item label="名称" class="formItem">
            {{ form.name }}
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="编码" class="formItem">
            {{ form.code }}
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="10">
        <a-col :span="8">
          <a-form-model-item label="开票名称" class="formItem">
            <a-input
              v-model="form.name2"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="税号" class="formItem">
            <a-input
              v-model="form.tin"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="电话" class="formItem">
            <a-input
              v-model="form.phone"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="地址" class="formItem">
            <a-input
              v-model="form.address"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="开户行" class="formItem">
            <a-input
              v-model="form.bank_name"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="开户账号" class="formItem">
            <a-input
              v-model="form.acc_number"
              :disabled="form.fp_partner_id ? true : false"
            />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-form-model-item :wrapper-col="{ span: 14, offset: 20 }">
        <a-button type="primary" @click="onUpdate" v-if="form.fp_partner_id">
          审核通过
        </a-button>
        <a-button type="primary" @click="onSubmit" v-else> 提交 </a-button>
      </a-form-model-item>
    </a-form-model>
    <a-divider />
    <div>
      <a-input-search
        placeholder="输入搜索内容"
        style="width: 200px"
        @search="onSearch"
      />

      <a-table
        :columns="columns"
        :data-source="records"
        rowKey="id"
        :customRow="tableCustomRow"
      >
      </a-table>
    </div>
  </div>
</template>

<script>
// todo TEST
import api from '@/odoorpc'

const columns = [
  // {
  //   dataIndex: "id",
  //   key: "id",
  //   title: "ID",
  //   // ellipsis: true,
  // },
  {
    dataIndex: 'name',
    key: 'name',
    title: '公司名称'
    // ellipsis: true,
  },
  {
    dataIndex: 'code',
    key: 'code',
    title: '编码'
    // ellipsis: true,
  },
  {
    dataIndex: 'tin',
    key: 'tin',
    title: '税号'
    // ellipsis: true,
  },
  {
    dataIndex: 'bank_name',
    key: 'bank_name',
    title: '开户行'
    // ellipsis: true,
  },
  {
    dataIndex: 'acc_number',
    key: 'acc_number',
    title: '开户行账号'
    // ellipsis: true,
  },
  {
    dataIndex: 'address',
    key: 'address',
    title: '公司地址'
    // ellipsis: true,
  },
  {
    dataIndex: 'phone',
    key: 'phone',
    title: '电话'
    // ellipsis: true,
  }
]

export default {
  name: 'PtnForm',
  components: {},
  mixins: [],

  data() {
    return {
      columns,
      records: [],
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      form: {
        name: '',
        code: '',
        fp_partner_id: '',
        code2: '',
        name2: '',
        tin: '',
        address: '',
        phone: '',
        bank_name: '',
        acc_number: ''
      },
      customerInfo: {
        ncid: 0,
        id: 0,
        name: '',
        code: '',
        fp_partner_id: '',
        name2: '',
        code2: '',
        tin: '',
        address: '',
        phone: '',
        bank_name: '',
        acc_number: ''
      }
    }
  },

  computed: {},

  watch: {},

  async created() {},

  async mounted() {},

  methods: {
    tableCustomRow(record) {
      return {
        on: {
          // eslint-disable-next-line no-unused-vars
          click: event => {
            console.log(record)

            this.form = {
              ...this.form,
              name2: record.name,
              code2: record.code,
              tin: record.tin,
              bank_name: record.bank_name,
              acc_number: record.acc_number,
              address: record.address,
              phone: record.phone,
              fp_partner_id: record.id
            }
          } // 点击行
        }
      }
    },

    async init(pid) {
      const raed_one = async (model, ids, fields) => {
        const res = await api.env.model(model).read(ids, { fields })
        const one = res[0]
        return one
      }

      const biz = await raed_one('fp.biz.partner', pid, [
        'nc_partner_ids',
        'nc_state',
        'fp_partner_id',
        'code',
        'name'
      ])

      const ptn = biz.fp_partner_id
        ? await raed_one('fp.partner', biz.fp_partner_id[0], [
            'code',
            'name',
            'fapiao_name',
            'tin',
            'phone',
            'address',
            'bank_name',
            'acc_number'
          ])
        : {}

      const nc_ids = biz.nc_partner_ids
      const nc = await raed_one('fp.nc.ods.customer', nc_ids, [
        'state',
        'code',
        'name',
        'code2',
        'name2',
        'tin',
        'address',
        'phone',
        'bank_name',
        'acc_number'
      ])

      this.customerInfo.ncid = nc.id
      this.customerInfo.id = biz.id
      this.customerInfo.name = biz.name
      this.customerInfo.code = biz.code

      if (biz.fp_partner_id) {
        this.customerInfo.fp_partner_id = biz.fp_partner_id[0]
        this.customerInfo.name2 = ptn.name
        this.customerInfo.code2 = ptn.code
        this.customerInfo.tin = ptn.tin
        this.customerInfo.phone = ptn.phone
        this.customerInfo.address = ptn.address
        this.customerInfo.bank_name = ptn.bank_name
        this.customerInfo.acc_number = ptn.acc_number
      }
      console.log('=======', this.customerInfo)
      this.form = this.customerInfo
      // // ,me.name,me.ref,
      // //   打开 弹窗。显示  partner_info
      // //     点击 审核通过 按钮 call:
      // //       await api.env.model('fp.nc.ods.customer').write( nc_info.id, {state: 'done' })
      // //   关闭 弹窗
      // //   然后 在刷新 本页面
    },

    async _update_nc() {
      const model = 'fp.nc.ods.customer'
      const Model = api.env.model(model)
      const ncid = this.form.ncid
      const vals = {
        code2: this.form.code2,
        name2: this.form.name2,
        tin: this.form.tin,
        phone: this.form.phone,
        address: this.form.address,
        bank_name: this.form.bank_name,
        acc_number: this.form.acc_number
      }

      await Model.write(ncid, vals)
    },

    async _load_to_ods() {
      const model = 'fp.biz.partner'
      const Model = api.env.model(model)
      const { name, code, code2, name2 } = this.form
      const { tin, phone, address, bank_name, acc_number } = this.form

      const biz_id = this.form.id

      // console.log(this.form, name, name2)

      const search_fp_partner = async () => {
        const fp_partner_ids = await api.env
          .model('fp.partner')
          .search([['name', '=', name2]], { limit: 1 })

        if (fp_partner_ids.length) {
          return fp_partner_ids[0]
        }

        return await Model.create({
          name: name2,
          code: code2,
          tin,
          phone,
          address,
          bank_name,
          acc_number
        })
      }

      const fp_partner_id = await search_fp_partner()

      await Model.write(biz_id, { fp_partner_id: fp_partner_id })
      // read me
      const biz = await Model.read(biz_id)
      console.log(biz)
    },

    async onSubmit() {
      this.$refs.customerInfoForm.validate(async valid => {
        // if (valid) {
        //   console.log("submit!");
        // } else {
        //   console.log("error submit!!");
        //   return false;
        // }

        console.log('valid...', valid)
        console.log(' submit,,,form:::', this.form)

        await this._update_nc()
        await this._load_to_ods()

        const model = 'fp.nc.ods.customer'
        const Model = api.env.model(model)
        const ncid = this.form.ncid

        await Model.write(ncid, { state: 'done' })
        const res2 = await Model.read(ncid)
        console.log(res2)

        this.$emit('closeModal', false)
      })
    },

    async onUpdate() {
      console.log(' update,,,form:::', this.form)

      await this._update_nc()

      await api.env.model('fp.biz.partner').write(this.form.id, {
        fp_partner_id: this.form.fp_partner_id
      })

      const model = 'fp.nc.ods.customer'
      const Model = api.env.model(model)
      const ncid = this.form.ncid

      await Model.write(ncid, { state: 'done' })

      const res2 = await Model.read(ncid)

      console.log(res2)
      //
      this.$emit('closeModal', false)
    },

    async onSearch(value) {
      console.log(value)

      const domain = [
        '|',
        '|',
        ['name', 'ilike', value],
        ['code', 'ilike', value],
        ['tin', 'ilike', value]
      ]
      const fields = [
        'name',
        'code',
        'tin',
        'address',
        'phone',
        'bank_name',
        'acc_number'
      ]

      const ptns = await api.env
        .model('fp.partner')
        .search_read({ domain, fields })
      console.log(ptns)

      this.records = ptns
    }
  }
}
</script>

<style type="text/css" scoped>
.formStyle {
  padding-left: 20px;
  padding-right: 20px;
}
.formItem {
  /* background-color: red; */
  margin-bottom: 0px;
}
</style>
